import 'package:flutter/material.dart';
import 'package:im_app/im/page/view/component/UserIconView.dart';
import 'package:im_app/im/util/ImConstant.dart';

import '../../../pojo/ImPojo.dart';
import '../../../util/HexColor.dart';

/// 需要重构，变成九宫格图片
class GroupIconView extends StatelessWidget {
  final ImGroup group;
  final double iconSize;

  const GroupIconView({required this.group, this.iconSize = ImConstant.IM_USER_ICON_SIZE, super.key});

  @override
  Widget build(BuildContext context) {
    // return Image(image: const AssetImage("assets/img/head.jpg"), width: iconSize, height: iconSize,);
    return Container(
      width: iconSize+4,
      height: iconSize+4,
      decoration: BoxDecoration(
          color: Colors.white12,
          // borderRadius: const BorderRadius.all(Radius.circular(2)),
          border: Border.all(color: HexColor.fromHex("#EEEDED"), width: 1)),
      child: Center(child: _buildBody(),),
    );
  }

  Widget _buildBody() {
    int size = group.groupUserList!.length;
    int crossAxisCount = calcCrossAxisCount(size);
    if (size == 3) {
      return _build3Users();
    } else if (size == 5) {
      return _build5Users();
    } else if (size == 7) {
      return _build7Users();
    } else if (size == 8) {
      return _build8Users();
    }
    return GridView.builder(
        physics: const NeverScrollableScrollPhysics(),
        padding: size > 4 && size < 7 ? EdgeInsets.only(top: iconSize / 6, bottom: iconSize / 6) : const EdgeInsets.all(0), itemCount: size,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: crossAxisCount, mainAxisSpacing: 0, crossAxisSpacing: 0),
        itemBuilder: (BuildContext context, int index) => _buildUserIcon(group.groupUserList!.elementAt(index), iconSize/3));
  }

  Widget _build3Users() {
    var childrens = group.groupUserList!.map((groupUser)=> _buildUserIcon(groupUser, iconSize/2)).toList();
    childrens.insert(0, const SizedBox(width: 10, height: 10,));
    childrens.insert(2, const SizedBox(width: 10, height: 10,));
    return Wrap(direction: Axis.horizontal, children: childrens,);
  }

  Widget _build5Users() {
    var childrens = group.groupUserList!.map((groupUser)=> _buildUserIcon(groupUser, iconSize/3)).toList();
    childrens.insert(0, SizedBox(width: iconSize/6, height: 10,));
    childrens.insert(3, SizedBox(width: iconSize/6, height: 10,));
    return Wrap(direction: Axis.horizontal, children: childrens,);
  }

  Widget _build7Users() {
    var childrens = group.groupUserList!.map((groupUser)=> _buildUserIcon(groupUser, iconSize/3)).toList();
    childrens.insert(0, SizedBox(width: iconSize/3, height: 10,));
    childrens.insert(2, SizedBox(width: iconSize/3, height: 10,));
    return Wrap(direction: Axis.horizontal, children: childrens,);
  }

  Widget _build8Users() {
    var childrens = group.groupUserList!.map((groupUser)=> _buildUserIcon(groupUser, iconSize/3)).toList();
    childrens.insert(0, SizedBox(width: iconSize/6, height: 10,));
    childrens.insert(3, SizedBox(width: iconSize/6, height: 10,));
    return Wrap(direction: Axis.horizontal, children: childrens,);
  }

  Widget _buildUserIcon(ImGroupUser groupUser, double iconSize) {
    return UserIconView(
      user: groupUser.user!,
      iconSize: iconSize,
      radioSize: 2,
    );
  }

  int calcCrossAxisCount(int size) {
    if (size <= 4) {
      return 2;
    }
    return 3;
  }
}
